package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SpusaveInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:ahang
 * @create:2022-09-29 19:45
 * @Description:
 */
@Api(tags = "spu平台接口")
@RequestMapping("admin/product/")
@RestController
public class spuController {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    /**
     * 根据三级分类id获取sup分页列表
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("{page}/{limit}")
    public Result getSpuinfoPage(@PathVariable("page") Long page,
                                @PathVariable("limit") Long limit,
                                @RequestParam("category3Id") Long category3Id){
        Page<SpuInfo> lists = spuInfoService.getSpuinfoPage(page,limit,category3Id);
        return Result.ok(lists);
    }

    /**
     * sup大保存
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpusaveInfoVo spusaveInfoVo){
        spuInfoService.saveSpuInfo(spusaveInfoVo);
        return Result.ok();
    }

    /**
     * 根据spuId获取图片列表
     * @param spuId
     * @return
     */
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId){
        //优点:当数据库中的字段名变了之后,不需要修改代码
        LambdaQueryWrapper<SpuImage> wrapper = new LambdaQueryWrapper<SpuImage>()
                .eq(SpuImage::getSpuId, spuId);
        List<SpuImage> result = spuImageService.list(wrapper);
        return Result.ok(result);
    }

    /**
     * 根据spuId获取销售属性
     * @return
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> resule = spuSaleAttrService.getSpuSaleAttrList(spuId);
        return Result.ok(resule);
    }


}
